/**
 * 
 */
package com.android.hw.projectmenu;
import com.android.hw.projectmenu.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

/**
 * @author gaoshengpan
 *
 */
public class LcdTest extends Activity {
    private int num = 0;
    private ImageView[] images = new ImageView[7];
    boolean ch = false;
    private String TAG = "LcdTest";

    /**
     * 
     */
    public LcdTest() {
        // TODO Auto-generated constructor stub
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.led_test);
        images[0] = (ImageView) findViewById(R.id.parent_0);
        images[1] = (ImageView) findViewById(R.id.led_1);
        images[2] = (ImageView) findViewById(R.id.led_2);
        images[3] = (ImageView) findViewById(R.id.led_3);
        images[4] = (ImageView) findViewById(R.id.led_4);
        images[5] = (ImageView) findViewById(R.id.led_5);
        images[6] = (ImageView) findViewById(R.id.led_6);

    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        ch = false;
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
		Log.d(TAG, "----------->++1");
        Log.d(TAG, "----------->++action" + event.getKeyCode());

        if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN
                && event.getAction() != KeyEvent.ACTION_UP) {
            Log.d(TAG, "----------->++num+" + num);
            ++num;
            if (num > 5) {
                num = 0;
                this.finish();
            }
            if (num == 5) {
                ch = true;
            }
            switchColor(num, false);
            Log.d(TAG, "afterSwitch----------->++num+" + num);

        } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP
                && event.getAction() != KeyEvent.ACTION_UP) {
            num--;
            if (num < 0) {
                num = 0;
            }
            if (num == 4 && ch) {
                switchColor(num, true);

            } else {
                switchColor(num, false);
            }
            Log.d(TAG, "----------->++num+" + num);
        }
        return true;
    }

    private void switchColor(int i, boolean change) {
        switch (i) {
        case 0:
            images[0].setBackgroundColor(Color.WHITE);
            break;
        case 1:
            images[0].setBackgroundColor(Color.BLACK);
            break;
        case 2:
            images[0].setBackgroundColor(Color.RED);
            break;
        case 3:
            images[0].setBackgroundColor(Color.GREEN);
            break;
        case 4:
            if (ch) {
                images[0].setVisibility(View.VISIBLE);
                images[1].setVisibility(View.GONE);
                images[2].setVisibility(View.GONE);
                images[3].setVisibility(View.GONE);
                images[4].setVisibility(View.GONE);
                images[5].setVisibility(View.GONE);
                images[6].setVisibility(View.GONE);
            } else {
                images[0].setBackgroundColor(Color.BLUE);
            }
            break;
        case 5:
            images[0].setVisibility(View.GONE);
            images[1].setVisibility(View.VISIBLE);
            images[2].setVisibility(View.VISIBLE);
            images[3].setVisibility(View.VISIBLE);
            images[4].setVisibility(View.VISIBLE);
            images[5].setVisibility(View.VISIBLE);
            images[6].setVisibility(View.VISIBLE);
            break;

        default:
            break;
        }

    }

}
